package com.xbongbong.paas.help.subform;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.saas.domain.entity.ProductStockEntity;
import com.xbongbong.saas.enums.subform.ProductStockSubFormEnum;
import com.xbongbong.paas.help.subform.abstracts.AbstractTransferSubForm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author 魏荣杰
 * @date 2020/8/25 10:58
 * @since
 * @version
 */
@Component
public class ProductStockTransferSubForm extends AbstractTransferSubForm {

    private static final Logger LOG = LoggerFactory.getLogger(ProductStockTransferSubForm.class);

    public PaasFormDataEntity transferEntityToFormData(ProductStockEntity productStockEntity) {
        PaasFormDataEntity paasFormDataEntity = new PaasFormDataEntity();
        paasFormDataEntity.setId(productStockEntity.getId());
        paasFormDataEntity.setCorpid(productStockEntity.getCorpid());
        paasFormDataEntity.setDel(productStockEntity.getDel());
        paasFormDataEntity.setAddTime(productStockEntity.getAddTime());
        paasFormDataEntity.setUpdateTime(productStockEntity.getUpdateTime());

        JSONObject dbJson = JSONObject.parseObject(JSON.toJSONString(productStockEntity));

        JSONObject data = new JSONObject();
        ProductStockSubFormEnum[] productStockSubFormEnums = ProductStockSubFormEnum.values();
        for (ProductStockSubFormEnum productStockSubFormEnum : productStockSubFormEnums) {
            String dbAttr = productStockSubFormEnum.getDbAttr();
            String dataAttr = productStockSubFormEnum.getDataAttr();
            Integer fieldType = productStockSubFormEnum.getFieldType();
            formatDataForEs(dbJson, data, dbAttr, dataAttr, fieldType);
        }
        paasFormDataEntity.setData(data);

        return paasFormDataEntity;
    }

    public List<PaasFormDataEntity> transferEntityToFormDataList(List<ProductStockEntity> productStockEntities) {
        List<PaasFormDataEntity> paasFormDataEntities = new ArrayList<>();
        for (ProductStockEntity productStockEntity : productStockEntities) {
            PaasFormDataEntity paasFormDataEntity = transferEntityToFormData(productStockEntity);
            paasFormDataEntities.add(paasFormDataEntity);
        }
        return paasFormDataEntities;

    }

    public ProductStockEntity transferFormDataToEntity(PaasFormDataEntity paasFormDataEntity) {

        JSONObject data = paasFormDataEntity.getData();

        JSONObject dbData = new JSONObject();
        ProductStockSubFormEnum[] productStockSubFormEnums = ProductStockSubFormEnum.values();
        for (ProductStockSubFormEnum productStockSubFormEnum : productStockSubFormEnums) {
            String dbAttr = productStockSubFormEnum.getDbAttr();
            String dataAttr = productStockSubFormEnum.getDataAttr();
            Integer fieldType = productStockSubFormEnum.getFieldType();
            formatDataForDb(data, dbData, dbAttr, dataAttr, fieldType);
        }
        ProductStockEntity productStockEntity = JSON.parseObject(dbData.toJSONString(), ProductStockEntity.class);
        productStockEntity.setAddTime(paasFormDataEntity.getAddTime());
        productStockEntity.setUpdateTime(paasFormDataEntity.getUpdateTime());
        productStockEntity.setCorpid(paasFormDataEntity.getCorpid());
        productStockEntity.setDel(paasFormDataEntity.getDel());
        productStockEntity.setId(paasFormDataEntity.getId());

        return productStockEntity;
    }

    public List<ProductStockEntity> transferFormDataToEntityList(List<PaasFormDataEntity> paasFormDataEntities) {
        List<ProductStockEntity> productStockEntities = new ArrayList<>();
        for (PaasFormDataEntity paasFormDataEntity : paasFormDataEntities) {
            ProductStockEntity productStockEntity = transferFormDataToEntity(paasFormDataEntity);
            productStockEntities.add(productStockEntity);
        }
        return productStockEntities;

    }
}
